package database

import (
	"github.com/beego/beego/v2/client/orm"
	"github.com/beego/beego/v2/core/logs"
	"github.com/beego/beego/v2/server/web"
	_ "github.com/go-sql-driver/mysql"
	"time"
)

func InitMysql() {
	url := web.AppConfig.DefaultString("mysql::DefaultDB", "")
	var params = []orm.DBOption{
		orm.ConnMaxLifetime(60 * time.Second),
		orm.MaxIdleConnections(25),
		orm.MaxOpenConnections(100),
	}


	errRegDb := orm.RegisterDataBase("default", "mysql", url, params...)
	if errRegDb != nil {
		println("mysql数据库连接失败！")
	}

	orm.DefaultTimeLoc = time.UTC
	prc, _ := time.LoadLocation("PRC")
	err := orm.SetDataBaseTZ("default", prc)
	if err != nil {
		println(err.Error())
	}

	orm.Debug, _ = web.AppConfig.Bool("orm::OrmDebug")
	orm.DebugLog = orm.NewLog(logs.GetLogger("DB").Writer())
}
